package Listener;

import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.ListSelectionModel;
import javax.swing.SwingUtilities;
import javax.swing.JTable;

import Model.ListeUtilisateur;

public class LMouseJTableRightClick implements MouseListener {

	ListeUtilisateur _listUser;
	
	public LMouseJTableRightClick(ListeUtilisateur listUser) {
		_listUser = listUser;
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {
		// Left mouse click
		if ( SwingUtilities.isRightMouseButton( e ) )
		{
			// get the coordinates of the mouse click
			Point p = e.getPoint();
 
			// get the row index that contains that coordinate
			int rowNumber = ((JTable)e.getSource()).rowAtPoint( p );
			
			// display contextual PopUp
			JPopupMenu menuSupp = new JPopupMenu();
			JMenuItem anItem = new JMenuItem("Suppression"); 
			
			anItem.addMouseListener(new LPopupSupp(_listUser, rowNumber));
			
			menuSupp.add(anItem);
	        menuSupp.show(e.getComponent(), e.getX(), e.getY()); 

			// Get the ListSelectionModel of the JTable
			ListSelectionModel model = ((JTable)e.getSource()).getSelectionModel();
 
			// set the selected interval of rows. Using the "rowNumber"
			// variable for the beginning and end selects only that one row.
			model.setSelectionInterval( rowNumber, rowNumber );
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	
}
